﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using SpiderPlatform.Entity;
using System.Linq;
using SpiderPlatform.Common;
using SpiderPlatform.Dao;
using System.Net;
using System.Threading.Tasks;
using System.Net.Http;

namespace SpiderPlatform
{
    public class MappingTools
    {
        ItemDao dao = new ItemDao();
        public void doWork()
        {
            List<BindingEntity> list = new List<BindingEntity>();            
            string[] tempData = new string[0];
            using (StreamReader sr = new StreamReader("c:\\mp.txt"))
            {
            
                while (!sr.EndOfStream)
                {
                    tempData = sr.ReadLine().Split(',');
                    if (tempData.Length==3)
                    {
                        list.Add(new BindingEntity {
                            item_number = tempData[0],
                            channel_sku= tempData[1],
                            channel_type= tempData[2]
                        });                       
                    }
                }
            }
            var gp = list.GroupBy(p => p.item_number);
            Logger.Info($"共有{gp.Count()}组商品");
            foreach (var itemList in gp)
            {
                List<BindingEntity> pram = new List<BindingEntity>();
                pram.Add(new BindingEntity
                {
                    channel_sku = itemList.Key,
                    channel_type ="yamibuy",
                    is_main = "Y"
                }); ;
                foreach (var item in itemList)
                {
                    pram.Add(new BindingEntity
                    {
                        channel_sku = item.channel_sku,
                        channel_type=item.channel_type,
                        is_main = "N"
                    });
                }
                Logger.Info("process =>" + itemList.Key);
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(pram);
                var result=HttpHelper.PostUrl("http://save.ecnow.tech/im/items/binding/0", str);               
                Logger.Info($"process =>{itemList.Key} OK");
            }
            Logger.Info("导入完成");
        }

        public void downloadData()
        {
            List<ApiData> list = new List<ApiData>();     
            do
            {
                list = dao.QueryApiData();
                List<Task> taskList = new List<Task>();
                foreach (var item in list)
                {
                    var task=Task.Factory.StartNew(async ()=> {
                        Logger.Info($"process {item.upc}");
                        HttpClient client = new HttpClient();
                        item.info = await client.GetStringAsync($"https://way.jd.com/jisuapi/barcode2?barcode={item.upc}&appkey=a78e5ce05956dff27b96a805497cc0f7");
                        Logger.Info($"download {item.upc} ok");
                    });
                    taskList.Add(task);
                }
                Task.WaitAll(taskList.ToArray());
                if (list.Count>0)
                {
                    dao.UpdateApiData(list);
                }           
                Logger.Info($"batch update success");
            } while (list.Count>0);
           
            Logger.Info("导入完成");
        }
    }
}
